<?php

namespace App\Admin\Controllers;

use App\Models\QnrCls;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Tree;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Str;

class QnrClsController extends AdminController
{

    public function index(Content $content)
    {
        return $content->header(__('qnr-cls.labels.QnrCls'))
            ->description('列表')
            ->body(function(Row $row) {

                $tree = new Tree(new QnrCls);

                $tree->query(function($model) {
                    return $model->orderBy('id','asc');
                });

                $tree->expand();

                $row->column(12,$tree);

            });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return $id;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new QnrCls(),function(Form $form) {

            $form->disableResetButton();

            $form->disableEditingCheck();

            $form->disableViewCheck();

            $form->hidden('c_key');

            $form->select('parent_id')->options(QnrCls::selectOptions())->required();

            $form->text('title')->required()->rules('min:1');

            $form->number('sort_id')
                ->default(1)
                ->type('number')
                ->attribute('min',1)
                ->attribute('max',60);

            $form->editor('content')->height(800);

            $form->radio('is_using')->options([1 => '开启',0 => '禁用'])->default(1)->required()->rules('min:1');

            // 保存时
            $form->saving(function(Form $form) {

                if(empty($this->c_key)) {
                    $form->c_key = Str::uuid();
                }

            });

        });
    }
}